package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.FreeHed;
import com.datamation.swadeshi.model.OrdDet;

public class FreeHedDS {

	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public FreeHedDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}

	
	@SuppressWarnings("static-access")
	public int createOrUpdateFreeHed(ArrayList<FreeHed>  list) {
		
		int returnID =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
					
    for (FreeHed hed : list) {
		
			ContentValues values = new ContentValues();
			
			values.put(dbHelper.FFREEHED_REFNO, hed.getFFREEHED_REFNO());
			values.put(dbHelper.FFREEHED_TXNDATE, hed.getFFREEHED_TXNDATE());
			values.put(dbHelper.FFREEHED_DISC_DESC, hed.getFFREEHED_DISC_DESC());
			values.put(dbHelper.FFREEHED_PRIORITY, hed.getFFREEHED_PRIORITY());
			values.put(dbHelper.FFREEHED_VDATEF, hed.getFFREEHED_VDATEF());
			values.put(dbHelper.FFREEHED_VDATET, hed.getFFREEHED_VDATET());
			values.put(dbHelper.FFREEHED_REMARKS, hed.getFFREEHED_REMARKS());
			values.put(dbHelper.FFREEHED_RECORD_ID, hed.getFFREEHED_RECORD_ID());
			values.put(dbHelper.FFREEHED_ITEM_QTY, hed.getFFREEHED_ITEM_QTY());
			values.put(dbHelper.FFREEHED_FREE_IT_QTY, hed.getFFREEHED_FREE_IT_QTY());
			values.put(dbHelper.FFREEHED_FTYPE, hed.getFFREEHED_FTYPE());

			
			
			returnID = (int) dB.insert(dbHelper.TABLE_FFREEHED, null, values);

			}
		}catch (Exception e) {
		
			Log.v("Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return returnID;
				
	}
	
	
	public int deleteAll(){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FFREEHED, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FFREEHED, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	
	
	public ArrayList<FreeHed> getFreeIssueItemDetailByRefno(String itemCode) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<FreeHed> list = new ArrayList<FreeHed>();
		
		String selectQuery = "select * from ffreehed where refno in (select refno from ffreedet where itemcode='"+itemCode+"') and date('now') between vdatef and vdatet";
		
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			FreeHed freeHed=new FreeHed();
		
			freeHed.setFFREEHED_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_REFNO)));
			freeHed.setFFREEHED_TXNDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_TXNDATE)));
			freeHed.setFFREEHED_DISC_DESC(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_DISC_DESC)));
			freeHed.setFFREEHED_PRIORITY(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_PRIORITY)));
			freeHed.setFFREEHED_VDATEF(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_VDATEF)));
			freeHed.setFFREEHED_VDATET(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_VDATET)));
			freeHed.setFFREEHED_REMARKS(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_REMARKS)));
			freeHed.setFFREEHED_RECORD_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_RECORD_ID)));
			freeHed.setFFREEHED_ITEM_QTY(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_ITEM_QTY)));
			freeHed.setFFREEHED_FREE_IT_QTY(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_FREE_IT_QTY)));
			freeHed.setFFREEHED_FTYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FFREEHED_FTYPE)));
			
			list.add(freeHed);
			
		}
		
		return list;
	}
}
